// pages/home/home.js
var renshu = 10,acid,openid,mima = '',userinfo = '',sitesite
Page({

  data: {
    statusBarHeight:0,
    items: [
      {num:2,},
      {num:4,},
      {num:6,},
      {num:8,},
      {num:10,}],
    checked:false,
    text:'',
    ins:''
  },
  //监听单选框，人数
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    renshu = e.detail.value
    
    // const items = this.data.items
    // for (let i = 0, len = items.length; i < len; ++i) {
    //   items[i].checked = items[i].value === e.detail.value
    // }
    // this.setData({
    //   items
    // })
  },
  //监听input框里面的内容,密码
  expInput: function (e) {
    var that=this;
    console.log(e)
    if(that.data.checked==false){
      // console.log(11)
      this.setData({ 
        text: e.detail.value 
      })
      mima = e.detail.value 
    }else if(that.data.checked==true){
      // console.log(22)
    }
    // console.log(this.data.text)
  },
  //监听房间名称
  ins:function(e){
    this.setData({
      ins:e.detail.value
    })
    console.log(this.data.ins)
  },
  //监听switch，有无密码
  switch1Change: function (e) {
    var that=this;
    console.log(e.target.value)
    // that.data.checked==!that.data.checked
  },
  // 创建房间
  btn:function(){
    var that = this
    if(this.data.ins!=''&&renshu!=0){
      wx.request({
        url: 'https://www.juquduo.com/juquduo/gaming/constructed/create',
        data:{
          activityId:acid,
          openid:openid,
          avatar:userinfo.avatarUrl,
          nick_name:userinfo.nickName,
          roomName:this.data.ins,
          roomPwd:mima,
          roomNumber:renshu
        },
          success(res){
            console.log(res)
            if(res.data.Code=='200'){
              wx.navigateTo({
                url: './game?name='+that.data.ins+'&renshu='+renshu+'&id='+res.data.RoomId+'&acid='+acid+'&site='+sitesite,
              })
              wx.request({
                url: 'https://www.juquduo.com/juquduo/gaming/constructed/ready?',
                data:{
                  activityId:acid,
                  roomId:res.data.RoomId,
                  openid:openid
                },
                success(res){
                  console.log('玩家已点锁定',res)
                  locked = 1
                  that.loaduser()
                }
              })
            }else{
              wx.showToast({
                title: '创建失败，稍后再试',
              })
            }
          }
      })
    }else{
      wx.showToast({
        icon:'none',
        title: '房间信息不全，请补充完整',
      })
    }
      
  },
  onLoad: function (options) {
    var that=this;
    acid = options.id
    sitesite = options.site
    openid = wx.getStorageSync('openid')
    userinfo = wx.getStorageSync('headimg')
    wx.getSystemInfo({  
      success: e => {  
        // console.log(e)
        that.setData({
          statusBarHeight:e.statusBarHeight
        })
      }  
    })
  }
})